<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">菜名</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="输入你的菜品名称" lay-reqtext="菜品名不能为空" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">类型</label>
        <div class="layui-input-block">
            <select name="type" class="layui-input">
                <option value="荤类">荤类</option>
                <option value="素类">素类</option>
                <option value="糕点">糕点</option>
                <option value="其他">其他</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="text" name="price" placeholder="输入价格" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">今日数量</label>
        <div class="layui-input-block">
            <input type="text" name="dayNumber" placeholder="输入价格" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">logo地址</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="logo" name="logo">
                <i class="layui-icon">&#xe67c;</i>上传logo
            </button>
            <label class="layui-form-text fileData">文件名：</label>

        </div>
    </div>
    <div class="layui-form-item imgShow">
        <label class="layui-form-label">logo</label>
        <div class="layui-input-block">
            <img src = "" class="layui-text showImg" width="200px" height="150px"/>
            <button type="button" class="layui-btn uploadLogo"  name="logo">
                <i class="layui-icon">&#xe605;</i>确认上传
            </button>
        </div>


    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <input type="text" name="description" placeholder="请输入描述" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal submits" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    var filePath  = "";
    //用于处理验证用户多次上传文件所导致的问题。
    var uploadIndex = 0;

    //当页面加载时，隐藏file上传的logo图片
    window.onload = function(){
        $(".imgShow").css("display","none");
    }
    layui.use(['form','upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload
            $ = layui.$;
        //upload
        upload.render({
            elem: "#logo",
            url: "/uploads/file",
            multiple:false,
            field: "file",
            bindAction:".uploadLogo",
            accept: "images",
            done : function(res, index){
                uploadIndex += 1;
                var jsons = JSON.stringify(res);
                var datas = JSON.parse(jsons)
                filePath = datas.data;
                console.log("上传后回调："+ datas.data);
            },
            choose : function(obj){
                console.log("上传索引："+ uploadIndex)
                if(uploadIndex >0) {
                    layer.alert("您已经上传过文件了"), {
                        title: "温馨提示"
                    }, function () {

                        // 关闭弹出层
                        layer.close(index);
                    }
                } else{
                    obj.preview(function(index, file,result){
                        var fileName = file.name;
                        var size = subParseDouble((file.size/1024)/1024);
                        var subString = "文件名：" + fileName + "  ,大小："+ size+ "MB";
                        $(".fileData").text(subString);
                        $(".showImg").attr("src",result);
                        $(".imgShow").css("display","block");
                    });
                }

            },
            auto:false,
        })


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var datas = JSON.stringify(data.field);
            var parses = JSON.parse(datas);
            ajaxAdd(parses,filePath);
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            var index = layer.alert("成功", {
                title: '提交结果'
            }, function () {
                parent.location.reload()
                return true;
            });
            return false;
        });

        //转换MB
        function subParseDouble(val){
            var strs = val.toString();
            var str = strs.substring(0,strs.indexOf(".")+3);
            return str;
        }

        //Ajax
        function ajaxAdd(data,filePath){
            $.ajax({
                url: "/meal/add",
                data:{
                    name: data.name,
                    type: data.type,
                    price: data.price,
                    dayNumber: data.dayNumber,
                    logo: filePath,
                    description: data.description
                },
                method: "post",
                success : function(cgdata){
                    console.log(cgdata);
                }
            })
        }
    });
    $(function(){

    })
</script>
</body>
</html>